/*
 * 广州云徙科技有限公司源代码，版权归广州云徙科技有限公司所有。
 * 
 * 项目名称 : yxs-gateway
 * 创建日期 : 2017年3月29日
 * 修改历史 : 
 *     1. [2017年3月29日]创建文件 by tan.wenjuan
 */
package com.project.ds.sqlprovider;

import org.apache.ibatis.annotations.Param;

import com.github.pagehelper.StringUtil;

/**
 * @author tan.wenjuan
 */
public class ArticleMapperProvider {
    
    public String getArticleListByCategoryId(@Param("categoryId")Long categoryId,int pageSize,int pageNo){
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from b_article where publish_status = 'P' ");
        if(categoryId!=null){
            sql.append(" and category_id = #{categoryId} ");
        }
        sql.append(" order by publish_time desc ");
        if (pageSize > 0) {
            sql.append(" LIMIT ");
            sql.append((pageNo - 1) * pageSize);
            sql.append(",");
            sql.append(pageSize);
        }
        return sql.toString();
    }
    
    public String getArticleListByLabelId(@Param("labelId")Long labelId,int pageSize,int pageNo){
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from b_article where publish_status = 'P' and dr=0");
        if(labelId!=null){
            sql.append(" and label_id = #{labelId} ");
        }
        sql.append(" order by publish_time desc ");
        if (pageSize > 0) {
            sql.append(" LIMIT ");
            sql.append((pageNo - 1) * pageSize);
            sql.append(",");
            sql.append(pageSize);
        }
        return sql.toString();
    }
    
    public String getCollectionList(@Param("userId")Long userId,int pageSize,int pageNo){
        StringBuilder sql = new StringBuilder();
        sql.append(" select a.* from b_collection c");
        sql.append(" left join b_article a on c.article_id = a.id");
        sql.append(" WHERE c.user_id = #{userId}");
        sql.append(" order by c.create_time desc ");
        if (pageSize > 0) {
            sql.append(" LIMIT ");
            sql.append((pageNo - 1) * pageSize);
            sql.append(",");
            sql.append(pageSize);
        }
        return sql.toString();
    }
    
    public String getArticleListByTitle(String title,int pageSize,int pageNo){
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from b_article where publish_status = 'P' ");
        if(StringUtil.isNotEmpty(title)){
            sql.append(" and title like '%"+title+"%'");
        }
        sql.append(" order by publish_time desc ");
        if (pageSize > 0) {
            sql.append(" LIMIT ");
            sql.append((pageNo - 1) * pageSize);
            sql.append(",");
            sql.append(pageSize);
        }
        return sql.toString();
    }
    
    public String getArticlePhotoList(@Param("categoryId")Long categoryId){
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,cover_pic_url from b_article where publish_status = 'P' ");
        if(categoryId!=null){
            sql.append(" and category_id = #{categoryId} ");
        }
        sql.append(" order by publish_time desc limit 5");
        return sql.toString();
    }
    
    public String getArticlePhotoListByLabelId(@Param("labelId")Long labelId){
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,cover_pic_url from b_article where publish_status = 'P' ");
        if(labelId!=null){
            sql.append(" and label_id = #{labelId} ");
        }
        sql.append(" order by publish_time desc limit 5");
        return sql.toString();
    }
}
